<template>
  <div class="set">
    <Row type="flex"
         align="middle"
         justify="center"
         class="set_ct">
      <Col>
      <Form ref="formDynamic" style="width:425px" v-show='loaded'
            :model="formDynamic"
            :rules="ruleValidate"
            :label-width="120">
           <FormItem label="配置开关：">
          <i-switch  size="large" v-model='switchStatus'>
            <span slot="open">开启</span>
            <span slot="close">关闭</span>
          </i-switch>
        </FormItem>
        <FormItem label="打印彩票下限：" v-show='switchStatus'
                  prop='notMember'>
          <!-- <Input v-model="formDynamic.notMember"  style='width:280px' clearable :maxlength="6"></Input> -->
          <Input :maxlength="6"
                 v-model="formDynamic.notMember"
                 @on-keyup="changecoinmember"
                 @on-blur="blurcoinmember"
                 style="width:280px"
                 placeholder='输入范围1~999999之间'></Input>
        </FormItem>
        <FormItem label="小票有效期天数：" v-show='switchStatus'
                  prop='depositTicketEffectiveDate'>
          <!-- <Input v-model="formDynamic.depositTicketEffectiveDate" style="width:280px" clearable :maxlength="3"></Input> -->
          <Input :maxlength="3"
                 v-model="formDynamic.depositTicketEffectiveDate"
                 style="width:280px"
                 @on-keyup="changedate"
                 @on-blur="blurchangedate"
                 placeholder='输入范围为1~999天'></Input>
          <span style="margin-left:10px">天</span>
        </FormItem>
        <FormItem>
          <Button type="primary"
                  @click="handleSubmit('formDynamic')"
                  class="button">确定</Button>
          <Button @click="cancel"
                  class="button btn1">取消</Button>
        </FormItem>
      </Form>
      </Col>
    </Row>
  </div>
</template>
<script>
import './index.less'
// import { routeEqual } from '@/libs/util'
import { getDicQuery, setDicSave } from '@/api/setting'
export default {
  data () {
    return {
      loaded: false,
      switchStatus: true,
      inputValue: '',
      formDynamic: {
        notMember: '200',
        depositTicketEffectiveDate: '1'
      },
      ruleValidate: {
        notMember: [{ required: true, message: '打印彩票下限不能为空', trigger: 'blur' }, { required: true, message: '输入范围1~999999之间', trigger: 'blur', pattern: /^[1-9][0-9]{0,5}$/ }],
        depositTicketEffectiveDate: [{ required: true, message: '小票有效期天数不能为空', trigger: 'blur' }, { required: true, message: '输入范围为1~999天', trigger: 'blur', pattern: /^[1-9][0-9]{0,2}$/ }]
      },
      paramter: {
        'shopId': this.$route.query.id,
        'merchantId': this.$store.state.user.merchantId,
        'msType': '1'
      }
    }
  },
  methods: {
    changecoinmember () {
      this.formDynamic.notMember = this.formDynamic.notMember.replace(/[^\d]/g, '').replace(/^0{1,}/g, '')
    },
    blurcoinmember () {
      this.formDynamic.notMember = this.formDynamic.notMember.replace(/[^\d]/g, '').replace(/^0{1,}/g, '')
    },
    blurchangedate () {
      this.formDynamic.depositTicketEffectiveDate = this.formDynamic.depositTicketEffectiveDate.replace(/[^\d]/g, '').replace(/^0{1,}/g, '')
    },
    changedate () {
      this.formDynamic.depositTicketEffectiveDate = this.formDynamic.depositTicketEffectiveDate.replace(/[^\d]/g, '').replace(/^0{1,}/g, '')
    },
    handleSubmit (name) {
      this.$refs[name].validate((valid) => {
        if (valid) {
          let data = Object.assign({}, this.paramter, { 'printReceiptCancel': this.formDynamic.notMember, 'depositTicketEffectiveDate': this.formDynamic.depositTicketEffectiveDate, 'ticketPrintSwitch': this.switchStatus ? '1' : '0' })
          setDicSave(data).then((res) => {
            if (res.data.status === 200) {
              this.$Message.success('设置成功!')
              this.closeModal()
            } else {
              this.$Message.error(`${res.data.error}`)
            }
          })
        }
      })
    },
    closeModal () {
      this.$store.commit('closeTag', this.$route)
      // this.$store.commit('setTagNavList', this.$store.state.app.tagNavList.filter(item => !routeEqual(this.$route, item)))
      // this.$nextTick(() => {
      //   this.$router.push({
      //     name: 'store_setting', params: { id: this.$route.query.id }
      //   })
      // })
      if (this.$store.state.app.tagNavList[this.$store.state.app.tagNavList.length - 1].name !== 'store_setting') {
        this.$nextTick(() => {
          this.$router.push({
            name: 'store_setting', params: { id: this.$route.query.id }
          })
        })
      }
    },
    cancel () {
      // this.$refs.formDynamic.resetFields()
      // this.formDynamic.notMember = null
      // this.formDynamic.depositTicketEffectiveDate = null
      this.closeModal()
    }
  },
  mounted () {
    let obj = Object.assign({}, this.paramter, { 'dictKeys': 'printReceiptCancel,depositTicketEffectiveDate,ticketPrintSwitch' })
    getDicQuery(obj).then((res) => {
      this.loaded = true
      if (res.data.status === 200) {
        if (res.data.data[0].printReceiptCancel === '0' || res.data.data[0].printReceiptCancel === 0) {
          this.formDynamic.notMember = '200'
        } else {
          this.formDynamic.notMember = String(res.data.data[0].printReceiptCancel)
        }
        if (res.data.data[1].depositTicketEffectiveDate === '0' || res.data.data[1].depositTicketEffectiveDate === 0) {
          this.formDynamic.depositTicketEffectiveDate = '1'
        } else {
          this.formDynamic.depositTicketEffectiveDate = String(res.data.data[1].depositTicketEffectiveDate)
        }
        if (res.data.data[2].ticketPrintSwitch === '0' || res.data.data[2].ticketPrintSwitch === 0) {
          this.switchStatus = false
        } else {
          this.switchStatus = true
        }
      } else {
        this.$Message.error(`${res.data.error}`)
      }
    })
    if (!this.hasPermission('moms:setting:customerticketprint')) {
      this.$router.replace({ name: 'error_401' })
    }
  }
}
</script>
